'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
    // 获取直播间列表
    async index() {
        const { ctx, app } = this;
        const { man, state } = ctx.query;
        let list = []
        if (JSON.parse(state) == null) {
            if (isNaN(man)) {
                list = await app.mysql.select('live', {
                    where: { man }
                })
            } else {
                list = await app.mysql.select('live', {
                    where: { id: man * 1 }
                })
            }
        } else {
            if (isNaN(man)) {
                list = await app.mysql.select('live', {
                    where: { man, state }
                })
            } else {
                list = await app.mysql.select('live', {
                    where: { id: man * 1, state }
                })
            }
        }
        ctx.body = {
            code: 1,
            list
        };
    }

    // 删除直播间
    async del() {
        const { ctx, app } = this;
        await app.mysql.delete('live', ctx.query)
        ctx.body = {
            code: 1
        }
    }

    // 下播
    async close() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        await app.mysql.update('live', { id, state: 0 })
        ctx.body = {
            code: 1
        }
    }
}

module.exports = HomeController;